package cn.qingyun.gis.modules.youshang.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.youshang.entity.YsChannelAttachment;
import cn.qingyun.gis.modules.youshang.mapper.YsChannelAttachmentMapper;
import cn.qingyun.gis.modules.youshang.service.IYsChannelAttachmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 友商渠道勘误-附件表 服务实现类
 * </p>
 *
 * @author guest
 * @since 2023-09-12
 */
@Service
public class YsChannelAttachmentServiceImpl extends ServiceImpl<YsChannelAttachmentMapper, YsChannelAttachment> implements IYsChannelAttachmentService {
    @Resource
    private YsChannelAttachmentMapper ysChannelAttachmentMapper;

    @Override
    @Transactional
    public void upload(List<String> pictureList, String id) {
        //上传图片不为空
        try {
            int count = ysChannelAttachmentMapper.selectCount(new LambdaQueryWrapper<YsChannelAttachment>().eq(YsChannelAttachment::getYsChannelProofreadId, id));
            if (count > 0) { //附件列表中存在上次勘误上传图片
                ysChannelAttachmentMapper.delete(new LambdaQueryWrapper<YsChannelAttachment>().eq(YsChannelAttachment::getYsChannelProofreadId, id));
            }
            if (ObjectUtil.isNotEmpty(pictureList)) {
                List<YsChannelAttachment> ysChannelAttachmentListList = Lists.newArrayList();
                for (String picture : pictureList) {
                    YsChannelAttachment ysChannelAttachment = new YsChannelAttachment();
                    ysChannelAttachment.setYsChannelProofreadId(id);
                    ysChannelAttachment.setContent(picture);
                    ysChannelAttachment.setUploadTime(DateUtil.date());
                    ysChannelAttachmentListList.add(ysChannelAttachment);
                }
                this.saveBatch(ysChannelAttachmentListList);
            }
        } catch (Exception e) {
            throw new CommonException("图片上传失败");
        }
    }

    @Override
    public List<YsChannelAttachment> show(String id) {
        if (CharSequenceUtil.isEmpty(id)) {
            throw new CommonException("传入id不能为空");
        }
        //返回图片信息
        return ysChannelAttachmentMapper.selectList(new LambdaQueryWrapper<YsChannelAttachment>().eq(YsChannelAttachment::getYsChannelProofreadId, id));
    }

}
